<?php
namespace app\back\controller;
use app\common\controller\AdminBase;
use think\Loader;
use think\Request;
use think\Url;
use think\Controller;
class Commit extends AdminBase {
    /**
     * 菜单列表
     * @author Zcc<2351976426@qq.com>
     * @dateTime 2016-12
     * @return [type] [description]
     */
    public function index() {
  
            $ruleModel = Loader::model('commit');
            $lists = $ruleModel::where(array('zid'=>session('ext_user.zid')))->paginate(2);
            $this->assign('lists', $lists);
            $this->assign('pages', $lists->render());
            $ruleModel = Loader::model('User');
            $ruleRows = $ruleModel::select();;
            $this->assign('ruleRows', $ruleRows);
            return $this->fetch();
    }








    /**
     * 添加菜单
     * @author Zcc<2351976426@qq.com>
     * @dateTime 2016-12
     * @param string $value [description]
     */
    public function add() {

        $request = Request::instance();
        if ($request->isPost()) {
            $params = $request->param();
            $params['zid']=session('ext_user.zid');
            $time=$request->param('create_time');
            $params['create_time']= strtotime($time);
            $params['update_time']= strtotime($time);
            if (loader::validate('Commit')->scene('add')->check($params) === false) {
                return $this->error(loader::validate('Commit')->getError());
            }
            if (($CommitId = Loader::model('Commit')->save($params)) === false) {
                return $this->error(loader::model('Commit')->getError());
            }
             Loader::model('SystemLog')->record("申请体现,ID:[{$CommitId}]");
            return $this->success('申请体现成功', Url::build('back/Commit/index'));
        }

          $zid=session('ext_user.zid');
          $score=Loader::model('Order')->field('score,create_time')->where(array('zid'=>$zid))->select();     
          $sum=0;
          $jifen=0;
          $com=0;
          foreach ($score as $k => $v) {
               $startdate = strtotime(date('Ymd',$v['create_time']));
               $enddate = strtotime(date('Ymd',time()+86400));
               $i=(($enddate-$startdate)/86400)-1;
               $bili=pow((1-1/2000),$i);
               $score=$v['score']*$bili;
                $jifen=$jifen+$score;
               //$jifen1=round($rrr,2);
               $sum=$sum+$v['score'];
               $money1=round($sum-$jifen, 2);;
          }
         $commit=Loader::model('Commit')->field('money')->where(array('zid'=>$zid))->select();
           foreach ($commit as $k => $v1) {
            $com=$com+$v1['money'];
           }
         $money=$money1-$com;
          $this->assign('sum',$jifen);
          $this->assign('money',$money);
        return $this->fetch();
    }

    /**
     * 编辑
     * @author Zcc<2351976426@qq.com>
     * @dateTime 2016-12
     * @param  string $value [description]
     * @return [type] [description]
     */
    public function edit($id) {
        $ruleModel = Loader::model('Commit');
        $ruleRow = $ruleModel::get($id);
        if ($ruleRow === false) {
            $this->error('没有找到对应的数据');
        }

        $request = Request::instance();
        if ($request->isPost()) {
            $params = $request->param();
            $time=$request->param('update_time');
            $params['update_time']= strtotime($time);
            $params['id'] = $id;

            if (loader::validate('Commit')->scene('edit')->check($params) === false) {
                return $this->error(loader::validate('Commit')->getError());
            }
            if (Loader::model('Commit')->save($params, ['id' => $id]) === false) {
                return $this->error(loader::model('Commit')->getError());
            }
              Loader::model('SystemLog')->record("审核成功,ID:[{$id}]");
            return $this->success('审核成功', Url::build('back/Commit/index'));
        }

        $ruleModel = Loader::model('User');
        $ruleRows = $ruleModel::select();;
        $this->assign('ruleRows', $ruleRows);
        //dump($ruleRow);exit();
        $this->assign('ruleRow', $ruleRow);
        return $this->fetch();
    }
    /**
     * 删除
     * @author Zcc<2351976426@qq.com>
     * @dateTime 2016-12
     * @param  [type] $id [description]
     * @return [type] [description]
     */
    public function destroy() {

$request = Request::instance();
    if ($request->isPost()) {
        $id = $request->param('id');
        $RuleModel = Loader::model('Commit');
        if ($RuleModel->deleteRule($id) === false) {
            return $this->error($RuleModel->getError());
        }
          Loader::model('SystemLog')->record("删除申请列表,ID:[{$id}]");
        //return $this->success('菜单列表删除成功', Url::build('back/rule/index'));
          return ['msg'=>'申请列表删除成功','status'=>'1'];
    }
    }
}